package com.jiaai.cloud.base.util;


import com.jiaai.cloud.base.configurer.UploadFileConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * 上传工具类
 */

public class UploadUtil {
    private static final Logger log= LoggerFactory.getLogger(UploadUtil.class);
     public static  String copyFile(MultipartFile file, String directories)  {
             String newName="";
            String filePath = SpringUtil.getBean(UploadFileConfig.class).getSavePath();
            try {
                newName=UUID.randomUUID() + getExtName(file.getOriginalFilename(), '.');
                if (StringUtils.isEmpty(directories)) {

                    filePath += File.separator + "other" + File.separator + newName;
                } else {
                    filePath += File.separator + directories + File.separator + newName;
                }
            if (file.isEmpty()) {
                return null;
            }
            File dest=new File(filePath);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            log.info("file upload susscess [{}]",filePath);
            return newName;
        }catch (Exception e){
            log.info("file upload error [{}]",e.getMessage());
            return null;
        }
    }

    /**
     * 获取绶存目录

     * @param directories
     * @return
     */
    public static  String getCacheDir(String directories){
        String filePath = SpringUtil.getBean(UploadFileConfig.class).getCachePath();
        if (StringUtils.isEmpty(directories)) {

            filePath += File.separator + "other" + File.separator ;
        } else {
            filePath += File.separator + directories + File.separator;
        }

        return filePath;
    }

    public  static  String getHttpUrl(String name, String directories ){
        StringBuffer buffer=new StringBuffer();
        buffer.append(SpringUtil.getBean(UploadFileConfig.class).getHttp());
        buffer.append("/dwnload/img/");
        buffer.append(directories);
        buffer.append("?f="+name);
        return buffer.toString();
    }
    /**
     * 获取源文件
     * @param name
     * @param directories
     * @return
     */
    public static   String getFilePath(String name, String directories){

        String filePath = SpringUtil.getBean(UploadFileConfig.class).getSavePath();
        if (StringUtils.isEmpty(directories)) {

            filePath += File.separator + "other" + File.separator + name;
        } else {
            filePath += File.separator + directories + File.separator + name;
        }

        return filePath;
    }
    public   static String getExtName(String s, char split) {
        int i = s.indexOf(split);
        int leg = s.length();
        return (i > 0 ? (i + 1) == leg ? " " : s.substring(i, s.length()) : " ");
    }
}
